package com.qingyouchuxing.mall.biz.controller;

import java.util.List;

import com.qingyouchuxing.mall.biz.domain.bo.MallGoodsBO;
import com.qingyouchuxing.mall.biz.domain.dto.MallGoodsDTO;
import com.qingyouchuxing.mall.biz.domain.dto.MallGoodsFromDTO;
import com.qingyouchuxing.mall.biz.domain.dto.MallGoodsStatusDTO;
import com.qingyouchuxing.mall.biz.domain.vo.MallGoodsVO;
import com.qingyouchuxing.mall.biz.service.IMallGoodsService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.web.annotation.RepeatSubmit;
import com.ruoyi.common.web.core.BaseController;

import com.ruoyi.common.orm.core.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author 数据小王子
 * 2024-09-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("mall/goods")
public class MallGoodsController extends BaseController
{
    @Autowired
    private IMallGoodsService mallGoodsService;


    /**
     * 修改商品上下架状态
     */
    @SaCheckPermission("mall:goods:editstatus")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/editstatus")
    public R<Void> editstatus(@Validated @RequestBody MallGoodsStatusDTO dto)
    {
        return toR(mallGoodsService.updateStatus(dto));
    }


    /**
     * 查询商品列表
     */
    @SaCheckPermission("mall:goods:list")
    @GetMapping("/page")
    public TableDataInfo<MallGoodsVO> page(MallGoodsDTO dto)
    {
        return mallGoodsService.selectPage(dto);
    }



    /**
     * 导出商品列表
     */
    @SaCheckPermission("mall:goods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallGoodsBO mallGoodsBo)
    {
        List<MallGoodsVO> list = mallGoodsService.selectList(mallGoodsBo);
        ExcelUtil.exportExcel(list, "商品", MallGoodsVO.class, response);
    }

    /**
     * 获取商品详细信息
     */
    @SaCheckPermission("mall:goods:query")
    @GetMapping(value = "/{id}")
    public R<MallGoodsVO> getInfo(@PathVariable Long id)
    {
        return R.ok(mallGoodsService.selectById(id));
    }

    /**
     * 新增商品
     */
    @SaCheckPermission("mall:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody MallGoodsFromDTO dto)
    {
        return toR(mallGoodsService.insert(dto));

    }

    /**
     * 修改商品
     */
    @SaCheckPermission("mall:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody MallGoodsFromDTO dto)
    {
        return toR(mallGoodsService.update(dto));
    }

    /**
     * 删除商品
     */
    @SaCheckPermission("mall:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids)
    {
        boolean deleted = mallGoodsService.deleteByIds(ids);
        if (!deleted) {
            return R.fail("删除商品记录失败!");
        }
        return R.ok();
    }
}
